<template>
  <el-form
    ref="loginForm"
    :rules="loginRules"
    :model="loginForm"
    class="login-form"
    status-icon
    label-width="0"
  >
    <h3 style="color: #1e9fff;text-align: center">报工登录</h3>

    <el-form-item prop="jobNo">
      <el-input
        v-model="loginForm.jobNo"
        size="small"
        auto-complete="off"
        placeholder="请输入员工号"
        @keyup.enter.native="handleLogin"
      >
        <i slot="prefix" class="icon-yonghuming" />
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        size="small"
        class="login-submit"
        @click.native.prevent="handleLogin"
      >登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from 'vuex'
import request from '@/router/axios'
import PageRouter from '@/router/page/index'
import {getPersonCredentialList} from "@/api/workorder/personcredentialmanage";
import {getAllUser} from "@/api/admin/user";
export default {
  name: 'reportLogin',
  data() {
    return {
      loginForm: {
        jobNo: '',
      },
      loginRules: {
        jobNo: [
          { required: true, message: '请输入员工号', trigger: 'blur' },
        ],
      },
      personList:[],
      userList:[],
    }
  },
  computed: {
  },
  mounted() {
  },
  created() {
    //获取所有员工
    this.getPersonCredentialListData();
    //获取所有用户
    this.getUserList();
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 合并判断员工号的方法
          const isJobNoExists = this.personList.some(person => person.jobNo === this.loginForm.jobNo) ||
            this.userList.some(user => user.username === this.loginForm.jobNo);
          if (isJobNoExists) {
            //如果是this.personList这里面的取出personName如果是this.userList中的取出name放入名称变量中
            let name = null;
            const person = this.personList.find(person => person.jobNo === this.loginForm.jobNo);
            if (person) {
              name = person.personName;
            }
            if (!name) {
              const user = this.userList.find(user => user.username === this.loginForm.jobNo);
              if (user) {
                name = user.name;
              }
            }
            this.$router.push({ path: '/workOrderReport', query: { jobNo: this.loginForm.jobNo,jobName:name} });
          } else {
            this.$message({
              message: '输入的员工号既不是系统用户，也不是工厂员工号',
              type: 'warning',

            })
            return false;
          }
        }
      })
    },
    getPersonCredentialListData(){
      getPersonCredentialList().then(response => {
        this.personList=response.data.data;
      })
    },
    getUserList(){
      getAllUser().then(response => {
        this.userList=response.data.data;
      })
    },
  }
}
</script>

<style>
</style>
